package com.yunhai.web.controller.file;

import com.yunhai.common.annotation.Log;
import com.yunhai.common.core.controller.BaseController;
import com.yunhai.common.core.domain.AjaxResult;
import com.yunhai.common.core.page.TableDataInfo;
import com.yunhai.common.enums.BusinessType;
import com.yunhai.common.utils.poi.ExcelUtil;
import com.yunhai.system.domain.Banner;
import com.yunhai.system.service.BannerService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * banner图Controller
 *
 * @author ruoyi
 * @date 2021-01-16
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/file/banner")
public class BannerController extends BaseController {

    private final BannerService bannerService;

    /**
     * 查询banner图列表
     */
    @PreAuthorize("@ss.hasPermi('system:banner:list')")
    @GetMapping("/list")
    public TableDataInfo list(Banner banner) {
        startPage();
        List<Banner> list = bannerService.queryList(banner);
        return getDataTable(list);
    }

    /**
     * 导出banner图列表
     */
    @PreAuthorize("@ss.hasPermi('system:banner:export')")
    @Log(title = "banner图", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Banner banner) {
        List<Banner> list = bannerService.queryList(banner);
        ExcelUtil<Banner> util = new ExcelUtil<Banner>(Banner.class);
        return util.exportExcel(list, "banner");
    }

    /**
     * 获取banner图详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:banner:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bannerService.getById(id));
    }

    /**
     * 新增banner图
     */
    @PreAuthorize("@ss.hasPermi('system:banner:add')")
    @Log(title = "banner图", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Banner banner) {
        return toAjax(bannerService.save(banner) ? 1 : 0);
    }

    /**
     * 修改banner图
     */
    @PreAuthorize("@ss.hasPermi('system:banner:edit')")
    @Log(title = "banner图", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Banner banner) {
        return toAjax(bannerService.updateById(banner) ? 1 : 0);
    }

    /**
     * 删除banner图
     */
    @PreAuthorize("@ss.hasPermi('system:banner:remove')")
    @Log(title = "banner图", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bannerService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
